const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
const EventListener = function (obj) {
  obj = obj||{};
  let Register = {};
  obj.on = function (name, method) {
      if (Register.hasOwnProperty(name)) {
          Register[name].push(method)
      } else {
          Register[name] = [method]
      }
  };
  obj.emit = function (name) {
      let handlerList = Register[name];
      for (let i = 0; i < handlerList.length; i++) {
          let handler = handlerList[i];
          let args = [];
          for (let j = 1; j < arguments.length; j++) {
              args.push(arguments[j])
          }
          handler.apply(this, args)
      }
  };
  obj.off = function (name, method) {
      if (Register.hasOwnProperty(name)) {
          let handlerList = Register[name];
          for (let i = 0; i < handlerList.length; i++) {
              if (handlerList[i].toString() === method.toString()) {
                  handlerList.splice(i, 1)
              }
          }
      }
  };
  obj.destroy = function () {
      Register = {}
  };
  return obj;
};
export default EventListener;
// module.exports = {
//   formatTime,
//   EventListener
// }
